package com.dinggao.crossgo.core.util;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;

import java.util.UUID;

public class DeviceInfo {

	public static String getDeviceToken(Context ctx) {
		final TelephonyManager tm = (TelephonyManager) ctx
				.getSystemService(Context.TELEPHONY_SERVICE);
		String tmDevice = "";
		String tmSerial = "";
		final String androidId;
		if (ActivityCompat.checkSelfPermission(ctx, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
			tmDevice = "" + tm.getDeviceId();
			tmSerial = "" + tm.getSimSerialNumber();
		}
		androidId = ""
				+ android.provider.Settings.Secure.getString(
				ctx.getContentResolver(),
				android.provider.Settings.Secure.ANDROID_ID);
		UUID deviceUuid = new UUID(HashAlgorithms.FNVHash1(androidId),
				((long) HashAlgorithms.FNVHash1(tmDevice) << 32) |HashAlgorithms.FNVHash1(tmSerial));
		return deviceUuid.toString();
	}

	//版本名
	public static String getVersionName(Context context) {
		return getPackageInfo(context).versionName;
	}

	//版本号
	public static int getVersionCode(Context context) {
		return getPackageInfo(context).versionCode;
	}

	private static PackageInfo getPackageInfo(Context context) {
		PackageInfo pi = null;
		try {
			PackageManager pm = context.getPackageManager();
			pi = pm.getPackageInfo(context.getPackageName(),
					PackageManager.GET_CONFIGURATIONS);

			return pi;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pi;
	}
}